<template>
	<view class="banner">
		<headerSpaceBetweenVue>
			<template #icon>
				<text @click="toggleModel()">兑换历史</text>
			</template>
			<template #title>卡密兑换</template>
		</headerSpaceBetweenVue>
		<view class="card-redeem-page">
			<view class="input-area">
				<input class="card-input" v-model="fake" placeholder="请输入兑换卡密" placeholder-class="placeholder" />
				<view class="redeem-button" @click="useFake()">
					<text>立即兑换</text>
				</view>
			</view>
			<view class="rules-section">
				<text class="section-title">兑换规则</text>
				<view class="rule-item">
					<text class="rule-text">1.每个卡密仅可使用一次</text>
				</view>
				<view class="rule-item">
					<text class="rule-text">2.兑换成功后立即到账</text>
				</view>
				<view class="rule-item">
					<text class="rule-text">3.请勿将卡密告知他人</text>
				</view>
			</view>

			<view class="faq-section">
				<text class="section-title">常见问题</text>
				<view class="faq-item">
					<text class="question">Q:卡密输入后提示无效怎么办?</text>
					<text class="answer">A:请检查卡密是否输入正确，确保没有多余的空格。</text>
				</view>
				<view class="faq-item">
					<text class="question">Q:兑换后没有收到奖励怎么办?</text>
					<text class="answer">A:请刷新页面或重新登录，如仍未到账请联系客服。</text>
				</view>
			</view>
			<navigator url="/pages/link/link">
				<view class="customer-service">
					<text>联系客服</text>
				</view>
			</navigator>
		</view>
		<view class="useLogModel" v-if="modelShow">
			<view class="logList">
				<view class="closeModel">
					<uni-icons type="closeempty" @click="closeModel()" size="30"></uni-icons>
				</view>
				<fakeItemVue v-for="(item,index) in list" :item="item" :key="index"></fakeItemVue>
				<uni-load-more :status="hasMore && isLoading ? 'loading' : 'noMore'"></uni-load-more>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';
	import {
		onLoad,
		onReachBottom
	} from "@dcloudio/uni-app"
	import headerSpaceBetweenVue from '@/components/header-space-between/header-space-between.vue';
	import fakeItemVue from '@/components/fake-item/fake-item.vue';
	import {
		apiFakeList,
		apiUseFake
	} from '@/api/apis';
	let fake = ref('')
	let page = ref(1)
	let list = ref([])
	let hasMore = ref(true)
	let isLoading = ref(false)
	let modelShow = ref(false)
	onLoad(() => {
		getFakeList()
	})
	const toggleModel = () => {
		modelShow.value = !modelShow.value
	}
	const closeModel = () => {
		modelShow.value = false
	}
	const getFakeList = async (query) => {
		await apiFakeList(query).then(res => {
			list.value = res.data.data
		})
	}
	const useFake = async () => {
		if (fake.value == '') {
			uni.showToast({
				icon: 'none',
				title: '卡密不能为空'
			})
			return
		}
		await apiUseFake({
			fake: fake.value

		}).then(res => {
			uni.showToast({
				title: res.msg
			})
		})
	}
	onReachBottom(() => {
		if (isLoading.value || !hasMore.value) return
		isLoading.value = true
		page.value = page.value + 1
		apiFakeList({
			page: page.value
		}).then(res => {
			if (res.data.data.length > 0) {
				isLoading.value = false
				list.value = [...list.value, ...res.data.data]
				if (!res.data.has_more) hasMore.value = false
			} else {
				hasMore.value = false
				isLoading.value = false
			}
		})

	})
</script>

<style lang="scss" scoped>
	$text-dark: #333333;
	$text-gray: #666666;
	$light-gray: #f0f0f0;

	.useLogModel {
		position: absolute;
		left: 0;
		right: 0;
		bottom: 0;
		top: calc(var(--status-bar-height) + 90rpx);

		height: calc(100vh - var(--status-bar-height) - 90rpx);
		background-color: rgba(0, 0, 0, 0.3);
		display: flex;
		justify-content: center;
		align-items: center;

		.logList {
			width: 80%;
			background-color: #ffffff;
			height: 80%;
			overflow: auto;
			position: relative;

			.closeModel {
				display: flex;
				padding: 10rpx;
				justify-content: flex-end;
				position: sticky;
				top: 0;
				left: 0;
				right: 0;
				background-color: #fff;
			}
		}
	}

	.banner {
		position: relative;
		padding: 20rpx;
		padding-top: calc(var(--status-bar-height) + 100rpx);

		.card-redeem-page {
			background-color: #ffffff;
			padding: 32rpx;
			box-sizing: border-box;
			display: flex;
			flex-direction: column;

			.input-area {
				display: flex;
				flex-direction: column;
				margin-bottom: 48rpx;

				.card-input {
					height: 88rpx;
					border: 2rpx solid $theme-color;
					border-radius: 8rpx;
					padding: 0 24rpx;
					margin-bottom: 32rpx;
					font-size: 32rpx;
					color: $text-dark;
				}

				.placeholder {
					color: #cccccc;
					font-size: 30rpx;
				}

				.redeem-button {
					height: 96rpx;
					background-color: $theme-color;
					border-radius: 8rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					color: white;
					font-size: 36rpx;
					font-weight: 500;
				}
			}

			.section-title {
				font-size: 36rpx;
				color: $text-dark;
				font-weight: bold;
				margin-bottom: 24rpx;
				display: block;
			}

			.rules-section {
				margin-bottom: 60rpx;

				.rule-item {
					margin-bottom: 20rpx;

					.rule-text {
						font-size: 32rpx;
						color: $text-gray;
					}
				}
			}

			.faq-section {
				margin-bottom: 40rpx;

				.faq-item {
					margin-bottom: 36rpx;

					.question {
						font-size: 32rpx;
						color: $text-dark;
						font-weight: 500;
						display: block;
						margin-bottom: 12rpx;
					}

					.answer {
						font-size: 30rpx;
						color: $text-gray;
						display: block;
						line-height: 1.6;
					}
				}
			}

			.customer-service {
				position: fixed;
				bottom: 60rpx;
				left: 0;
				right: 0;
				display: flex;
				justify-content: center;

				text {
					color: $theme-color;
					font-size: 32rpx;
				}
			}
		}
	}
</style>